<template>
  <el-dialog
    :before-close="close"
    :width="dialogWidth"
    :title="title"
    :visible.sync="isShow"
    v-if="isShow"
    center
    v-dialogDrag
  >
    <el-form :model="form" ref="form" :rules="rules" class="mt20">
      <el-col :span="12" class="mb20">
        <el-form-item label="时间：" prop="time" :label-width="formLabelWidth">
          <div class="block">
            <el-date-picker
              v-model="form.time"
              type="month"
              placeholder="选择时间"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </div>
        </el-form-item>
      </el-col>
      <el-col :span="12" class="mb20">
        <el-form-item label="中心：" prop="center" :label-width="formLabelWidth">
          <el-select placeholder="请选择中心名" v-model="form.center" @change="getDeptList">
            <el-option v-for="item in centerList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12" class="mb20">
        <el-form-item label="部门：" prop="department" :label-width="formLabelWidth">
          <el-select placeholder="请选择部门名" v-model="form.department">
            <el-option v-for="item in deptList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12" class="mb20">
        <el-form-item label="上门计划：" prop="visit_plan" label-width="100px">
          <el-input
            v-model="form.visit_plan"
            type="number"
            autocomplete="off"
            placeholder="请输入上门计划"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12" class="mb20">
        <el-form-item label="签单计划：" prop="sign_plan" label-width="100px">
          <el-input v-model="form.sign_plan" type="number" autocomplete="off" placeholder="请输入签单计划"></el-input>
        </el-form-item>
      </el-col>
    </el-form>
    <span slot="footer" class="dialog-footer align-center tiling inline-block mt20 mb20">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="handleConfirm('form')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import "../utils/drag.js";

export default {
  name: "plan_dialog",
  data() {
    var validateVisitPlan = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入上门计划！"));
      } else if (value.search(/^[0-9]*$/) === -1) {
        return callback(new Error("请输入数字！"));
      } else if (value.length > 5) {
        return callback(new Error("长度在 1-5 个数字！"));
      } else {
        callback();
      }
    };
    var validateSignPlan = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入签订计划！"));
      } else if (value.search(/^[0-9]*$/) === -1) {
        return callback(new Error("请输入数字！"));
      } else if (value.length > 5) {
        return callback(new Error("长度在 1-5 个数字！"));
      } else {
        callback();
      }
    };
    return {
      form: {
        time: "",
        visit_plan: "",
        sign_plan: "",
        center: "",
        department: ""
      },
      rules: {
        time: { required: true, message: "请选择分配时间！", trigger: "blur" },
        center: {
          required: true,
          message: "请选择分配中心！",
          trigger: "blur"
        },
        department: {
          required: true,
          message: "请选择分配部门！",
          trigger: "blur"
        },
        visit_plan: { validator: validateVisitPlan, trigger: "blur" },
        sign_plan: { validator: validateSignPlan, trigger: "blur" }
      },
      centerList: [],
      deptList: [],
      formLabelWidth: "70px",
      dialogWidth: "680px"
    };
  },
  created() {
    this.getCenterList();
  },
  computed: {
    isShow() {
      return this.addDialog || this.editDialog;
    }
  },
  methods: {
    getDeptList() {
      this.form.dept = "";
      this.form.department = "";
      this.$axios.post("/api/system/departmentList", this.form).then(data => {
        this.deptList = data.map(item => {
          return item.name;
        });
      });
    },
    getCenterList() {
      this.$axios.get("/api/system/centers").then(data => {
        this.centerList = data.map(item => {
          return item.name;
        });
      });
    },
    handleConfirm(formName) {
      // 先进行表单验证
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.addPlan();
        }
      });
    },
    addPlan() {
      this.$axios.post("/api/business/addPlan", this.form).then(data => {
        if (data.status) {
          this.$message({
            type: "success",
            message: "添加成功！"
          });
          this.$emit("update");
        } else {
          this.$message({
            type: "error",
            message: data.msg
          });
        }
      });
    },
    close() {
      this.$emit("close");
      this.resetForm();
    },
    resetForm() {
      this.form = {
        time: "",
        visit_plan: "",
        sign_plan: "",
        center: "",
        department: ""
      };
    }
  },
  props: ["addDialog", "title", "planForm"]
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
